package com.ice.app.system.shortcut.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.util.StringUtil;
import com.ice.app.system.constants.CommonConstant;
import com.ice.app.system.entity.Shortcut;
import com.ice.app.system.mapper.ShortcutMapper;
import com.iceframework.core.utils.KeyUtils;
import com.iceframework.core.utils.WebUtils;

/**
 * 
 * 业务逻辑层： 快捷方式
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * @date 2020-02-10 07:31:28
 */
@Service
public class ShortcutService {

	@Autowired
	private ShortcutMapper shortcutMapper;

	// ----------------------------------------自动生成--------------------------------------------

	/**
	 * 菜单保存
	 * 
	 * @param shortcut
	 * @return
	 * @throws Exception
	 */
	@Transactional
	public boolean saveShortcut(Shortcut shortcut) throws Exception {
		if (StringUtil.isNotEmpty(shortcut.getId())) {
			// 修改操作
			Shortcut old = this.getShortcut(shortcut.getId());
			shortcut.setUpdateBy(WebUtils.getUserName());
			shortcut.setUpdateDate(new Date());
			if (old != null) {
				return shortcutMapper.updateByPrimaryKeySelective(shortcut) > 0;
			}
			return false;
		} else {
			// 保存操作
			String key = KeyUtils.genKey("Shortcut");
			shortcut.setId(key);
			shortcut.setCreateBy(WebUtils.getUserName());
			shortcut.setCreateDate(new Date());
			shortcut.setUpdateBy(WebUtils.getUserName());
			shortcut.setUpdateDate(new Date());
			shortcut.setUserId(WebUtils.getUserId());
			shortcut.setStatus(CommonConstant.COMMON_STATUS_VALID);
			return shortcutMapper.insertSelective(shortcut) > 0;

		}
	}

	/**
	 * 根据主键删除菜单
	 * 
	 * @param key
	 * @return
	 */
	@Transactional
	public boolean deleteShortcut(String key) {
		return shortcutMapper.deleteByPrimaryKey(key) > 0;
	}

	/**
	 * 根据多个主键删除菜单
	 * 
	 * @param keys
	 * @return
	 */
	@Transactional
	public boolean deleteShortcuts(String keys) {
		if (!StringUtils.isEmpty(keys)) {
			String[] strs = keys.split(",");
			for (String key : strs) {
				this.deleteShortcut(key);
			}
			return true;
		}
		return false;
	}

	/**
	 * 根据主键获取菜单实体
	 * 
	 * @param key
	 * @return
	 */
	public Shortcut getShortcut(String key) {
		return shortcutMapper.selectByPrimaryKey(key);
	}

	/**
	 * 根据给定条件获取菜单实体集合
	 * 
	 * @param key
	 * @return
	 */
	public List<Shortcut> getShortcuts(Shortcut shortcut) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shortcut", shortcut);
		return shortcutMapper.selectByFilter(map);
	}

	// ----------------------------------------自定义（Public）---------------------------------------------

}